import type { TimeRange } from './types'
import dayjs from 'dayjs'

import { isNil } from 'lodash-es'

/**
 * 秒转时间
 * @param second 秒
 * @returns
 */
export function getTimeBySecond(second?: number): TimeRange {
  if (isNil(second))
    return [undefined, undefined, undefined]
  const minute = Math.floor(second / 60)
  const hour = Math.floor(minute / 60)
  const h = hour > 0 ? hour : undefined
  const m = minute % 60 <= 0 && h === undefined ? undefined : minute % 60
  const s = ((second * 1000) % (60 * 1000)) / 1000
  return [h, m, s]
}

/**
 * 秒转换为时长字符串
 * @param second 秒
 * @returns
 */
export function getStrBySecond(second: number): string {
  if (!second)
    return '-'
  const minute = Math.floor(second / 60)
  return `${minute} 分钟`
  // const unit = ['小时', '分钟', '秒']
  // const [hour, minute, sec] = getTimeBySecond(second)
  // const str = `${hour ? hour + unit[0] : ''}${minute ? minute + unit[1] : ''}${
  //   sec ? sec + unit[2] : ''
  // }`
  // 添加间隙
  // return str.replace(/\d+/g, ($1) => ` ${$1} `).trim()
}

/**
 * 格式化时间
 */
export function formatDate(unix: string, template = 'YYYY-MM-DD HH:mm:ss') {
  return unix ? dayjs.unix(Number(unix)).format(template) : '-'
}
